from django.shortcuts import render
from .models import Movie

def movie_list(request):
    all_movies = Movie.objects.all().order_by('-created_at')  # 获取所有电影按创建时间排序
    total_movies = all_movies.count()

    # 获取当前页码，默认为0
    page = int(request.GET.get('page', 0))

    # 计算当前显示的5部电影
    start = page * 5
    end = start + 5

    # 如果超过总数，回到开头
    if start >= total_movies:
        start = 0
        end = 5
        page = 0

    movies = all_movies[start:end]

    context = {
        'movies': movies,
        'page': page,
        'total_movies': total_movies,
        'page_title': '热播电影'
    }
    return render(request, 'movies/movie_list.html', context)
